import BaseShape2D from "./BaseShape2D";
import Vector2 from "../maths/Vector2";
import ITransformable from "../interfaces/ITransformable";
import Math2D from "../maths/Math2D";
import IRenderState from "../interfaces/IRenderState";

export default class Rect extends BaseShape2D {
    // 位置与尺寸
    public width: number;
    public height: number;
    public x: number;
    public y: number;
    // 使用u、v控制矩形的原点偏移，其中u、v的取值范围为[0, 1]之间，表示原点相对矩形左上角的偏移比例
    public get right(): number {
        return this.x + this.width;
    }

    public get bottom(): number {
        return this.y + this.height;
    }

    // 覆写基类的type属性
    public get type(): string {
        return "Rect";
    }

    public constructor(w: number = 1, h: number = 1, u: number = 0, v: number = 0) {
        super();
        this.width = w;
        this.height = h;
        this.x = - this.width * u;
        this.y = - this.height * v;
    }

    // 覆写基类hitTest方法，调用前面实现的isPointInRect方法
    public hitTest(localPt: Vector2, transform: ITransformable): boolean {
        return Math2D.isPointInRect(localPt.x, localPt.y, this.x, this.y, this.width, this.height);
    }

    // 复写基类draw方法，最后调用super . draw方法用来显示当前绘制的形体
    public draw(transformable: ITransformable, state: IRenderState, context: CanvasRenderingContext2D): void {
        context.beginPath();
        context.moveTo(this.x, this.y);
        context.lineTo(this.x + this.width, this.y);
        context.lineTo(this.x + this.width, this.y + this.height);
        context.lineTo(this.x, this.y + this.height);
        context.closePath();
        super.draw(transformable, state, context);
    }

}